package com.example.emptytest;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.emptytest.DB.SQLiteHelper;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

public class ForgetPswd extends AppCompatActivity {
    private ImageView mBtn_backhome;
    private TextView mBtn_reg;
    private TextView mBtn_intro;
    private EditText StNum;
    private EditText StPswd;
    private EditText StName;
    private SQLiteHelper helper;
    private Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pswd);
        //getSupportActionBar().hide();
        helper = new SQLiteHelper(this);
        initView();
    }
    private void initView(){
        mBtn_backhome = (ImageView)findViewById(R.id.btn_backhome);
        mBtn_intro = (TextView)findViewById(R.id.btn_intro);
        mBtn_reg = findViewById(R.id.btn_reg);
        StNum = findViewById(R.id.forget_StNum);
        StPswd = findViewById(R.id.forget_StPswd);
        StName = findViewById(R.id.forget_StName);
        // 返回
        mBtn_backhome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 关于注册
        mBtn_intro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ForgetPswd.this);
                builder.setTitle("关于注册");
                builder.setMessage("本系统绑定账号与教务网相同，注册时请输入教务网账号密码。" +
                        "注册完成后请不要重复注册。");
                builder.setNegativeButton("知道了",new DialogInterface.OnClickListener(){

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {

                    }
                });
                builder.create().show();
            }
        });
        // 注册按钮
        mBtn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //doPostReg(StNum,StPswd,StName);
                String Num = StNum.getText().toString();
                String Pswd = StPswd.getText().toString();
                String Name = StName.getText().toString();
                String args[] = new String[]{"%" + Num + "%"};
                cursor = helper.query(args);
                if(cursor.getCount() != 0)
                {
                    Toast toast0 = Toast.makeText(ForgetPswd.this, "账号已注册，请勿重复注册", Toast.LENGTH_SHORT);
                    toast0.setGravity(Gravity.CENTER, 0, 0);
                    toast0.show();
                }
                else{
                    if(Num.compareTo("") == 0 || Pswd.compareTo("") == 0 || Name.compareTo("") == 0){
                        Toast toast1 = Toast.makeText(ForgetPswd.this, "请勿输入空值", Toast.LENGTH_SHORT);
                        toast1.setGravity(Gravity.CENTER, 0, 0);
                        toast1.show();
                    }
                    else{
                        helper.insert(Num,Pswd,1,Name,"");
                        // 刷新
                        cursor.requery();
                        StNum.setText("");
                        StPswd.setText("");
                        StName.setText("");
                        Toast toast2 = Toast.makeText(ForgetPswd.this, "注册成功", Toast.LENGTH_SHORT);
                        toast2.setGravity(Gravity.CENTER, 0, 0);
                        toast2.show();
                    }

                }

            }
        });

    }

    private void doPostReg(EditText stNum, EditText stPswd, EditText stName) {
        String uriAPI = "";
        try{
            String Num = StNum.getText().toString();
            String Pswd = StPswd.getText().toString();
            String Name = StName.getText().toString();
            URL url = new URL(uriAPI);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(5000);
            connection.setRequestMethod("POST");
            // 数据
            String data = "user_name="+Num+"&password="+Pswd+"&name="+Name;
            // 请求头
            connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
            connection.setRequestProperty("Content-Type",data.length()+"");
            // post 提交
            connection.setDoInput(true);
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(data.getBytes());

            // 回复
            int responseCode = connection.getResponseCode();
            switch (responseCode){
                case 0:
                    Toast toast0 = Toast.makeText(ForgetPswd.this, "该账号已被注册", Toast.LENGTH_SHORT);
                    toast0.setGravity(Gravity.CENTER, 0, 0);
                    toast0.show();
                    break;
                case 1:
                    Toast toast1 = Toast.makeText(ForgetPswd.this, "账号注册成功", Toast.LENGTH_SHORT);
                    toast1.setGravity(Gravity.CENTER, 0, 0);
                    toast1.show();
                    break;
                case 2:
                    Toast toast2 = Toast.makeText(ForgetPswd.this, "未知错误", Toast.LENGTH_SHORT);
                    toast2.setGravity(Gravity.CENTER, 0, 0);
                    toast2.show();
                    break;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }
}